/*
 * @Author        : wang chao
 * @Date          : 2024-03-26 12:51:17
 * @LastEditors: wangchao
 * @LastEditTime: 2024-10-12 09:33:52
 * @FilePath: \JD-RTT-Driver\applications\u_alarm.h
 * @Description   :
 * @Copyright     : Copyright 2024 wang chao, All Rights Reserved.
 */
#ifndef _U_ALARM_H
#define _U_ALARM_H

#include <rtdevice.h>
#include <rtthread.h>

#define ALM_CONFIRM_TIME_30S   (60)
#define ALM_CONFIRM_TIME_10S   (20)
#define ALM_CONFIRM_TIME_5S    (10)
#define ALM_CONFIRM_TIME_3S    (6)
#define ALM_CONFIRM_TIME_2S    (4)
#define ALM_CONFIRM_TIME_1S    (2)
#define ALM_SCAN_TIME_INTERVAL (500)

#define SET_ALARM_WORD(word, bit, value) u_bitWrite(*(word), (bit), (value))

#define BMS_INIT_HIGH_POINT(bms, high_action, high_valid, high_start_stop_ignore, high_threshold, high_confirm_time) \
    do                                                                                                               \
    {                                                                                                                \
        (bms)->HIGH_ACTION = (high_action);                                                                          \
        (bms)->HIGH_VALID = (high_valid);                                                                            \
        (bms)->HIGH_START_STOP_IGNORE = (high_start_stop_ignore);                                                    \
        (bms)->HIGH_THRESHOLD = (high_threshold);                                                                    \
        (bms)->HIGH_CONFIRM_TIME = (high_confirm_time);                                                              \
    } while (0)

#define BMS_INIT_LOW_POINT(bms, low_action, low_valid, low_start_stop_ignore, low_threshold, low_confirm_time) \
    do                                                                                                         \
    {                                                                                                          \
        (bms)->LOW_ACTION = (low_action);                                                                      \
        (bms)->LOW_VALID = (low_valid);                                                                        \
        (bms)->LOW_START_STOP_IGNORE = (low_start_stop_ignore);                                                \
        (bms)->LOW_THRESHOLD = (low_threshold);                                                                \
        (bms)->LOW_CONFIRM_TIME = (low_confirm_time);                                                          \
    } while (0)

typedef struct
{
    /*******************************
     *         L1告警信息定义
     ********************************
     */
    // L1告警-高点信息
    rt_bool_t HIGH_ACTION;
    rt_bool_t HIGH_VALID;
    rt_bool_t HIGH_START_STOP_IGNORE;
    // L1告警-低点信息
    rt_bool_t LOW_ACTION;
    rt_bool_t LOW_VALID;
    rt_bool_t LOW_START_STOP_IGNORE;
    // L1告警-高低阈值
    rt_int16_t HIGH_THRESHOLD;
    rt_int16_t LOW_THRESHOLD;
    // L1告警-延时时间
    rt_int16_t CONFIRM_TIME;
    // L1告警-延时时间
    rt_int16_t HIGH_CONFIRM_TIME;
    // L1告警-延时时间
    rt_int16_t LOW_CONFIRM_TIME;
} BMS_L1_FAIL_TYPE;

typedef struct
{
    /*******************************
     *         L2告警信息定义
     ********************************
     */
    // L2告警-高点信息
    rt_bool_t HIGH_ACTION;
    rt_bool_t HIGH_VALID;
    rt_bool_t HIGH_START_STOP_IGNORE;
    // L2告警-低点信息
    rt_bool_t LOW_ACTION;
    rt_bool_t LOW_VALID;
    rt_bool_t LOW_START_STOP_IGNORE;
    // L2告警-高低阈值
    rt_int16_t HIGH_THRESHOLD;
    rt_int16_t LOW_THRESHOLD;
    // L2告警-延时时间
    rt_int16_t CONFIRM_TIME;
    // L2告警-延时时间
    rt_int16_t HIGH_CONFIRM_TIME;
    // L2告警-延时时间
    rt_int16_t LOW_CONFIRM_TIME;
} BMS_L2_FAIL_TYPE;

typedef struct
{
    /*******************************
     *         L3告警信息定义
     ********************************
     */
    // L3告警-高点信息
    rt_bool_t HIGH_ACTION;
    rt_bool_t HIGH_VALID;
    rt_bool_t HIGH_START_STOP_IGNORE;
    // L3告警-低点信息
    rt_bool_t LOW_ACTION;
    rt_bool_t LOW_VALID;
    rt_bool_t LOW_START_STOP_IGNORE;
    // L3告警-高低阈值
    rt_int16_t HIGH_THRESHOLD;
    rt_int16_t LOW_THRESHOLD;
    // L3告警-延时时间
    rt_int16_t CONFIRM_TIME;
    // L3告警-延时时间
    rt_int16_t HIGH_CONFIRM_TIME;
    // L3告警-延时时间
    rt_int16_t LOW_CONFIRM_TIME;
} BMS_L3_FAIL_TYPE;

typedef struct
{
    rt_uint16_t WORD01;
    rt_uint16_t WORD02;
    rt_uint16_t WORD03;
    rt_uint16_t WORD04;
    rt_uint16_t WORD05;
    rt_uint16_t WORD06;
} BMS_L1_FAIL_REPORT;

typedef union
{
    BMS_L1_FAIL_REPORT;
    rt_uint16_t WORDS[6];
} BMS_L1_FAIL_UNION;

typedef struct
{
    rt_uint16_t WORD01;
    rt_uint16_t WORD02;
    rt_uint16_t WORD03;
    rt_uint16_t WORD04;
    rt_uint16_t WORD05;
    rt_uint16_t WORD06;
} BMS_L2_FAIL_REPORT;

typedef union
{
    BMS_L2_FAIL_REPORT;
    rt_uint16_t WORDS[6];
} BMS_L2_FAIL_UNION;

typedef struct
{
    rt_uint16_t WORD01;
    rt_uint16_t WORD02;
    rt_uint16_t WORD03;
    rt_uint16_t WORD04;
    rt_uint16_t WORD05;
    rt_uint16_t WORD06;
} BMS_L3_FAIL_REPORT;

typedef union
{
    BMS_L3_FAIL_REPORT;
    rt_uint16_t WORDS[6];
} BMS_L3_FAIL_UNION;

typedef struct
{
    BMS_L1_FAIL_TYPE P1_Pump_Current;
    BMS_L1_FAIL_TYPE P2_Pump_Current;
    BMS_L1_FAIL_TYPE N1_Pump_Current;
    BMS_L1_FAIL_TYPE N2_Pump_Current;
    //
    BMS_L1_FAIL_TYPE P1_Electrolyte_Temp;
    BMS_L1_FAIL_TYPE P2_Electrolyte_Temp;
    BMS_L1_FAIL_TYPE N1_Electrolyte_Temp;
    BMS_L1_FAIL_TYPE N2_Electrolyte_Temp;
    //
    BMS_L1_FAIL_TYPE P1_StorageTank_Temp;
    BMS_L1_FAIL_TYPE P2_StorageTank_Temp;
    BMS_L1_FAIL_TYPE N1_StorageTank_Temp;
    BMS_L1_FAIL_TYPE N2_StorageTank_Temp;
    //
    BMS_L1_FAIL_TYPE P1_Flow;
    BMS_L1_FAIL_TYPE P2_Flow;
    BMS_L1_FAIL_TYPE N1_Flow;
    BMS_L1_FAIL_TYPE N2_Flow;
    //
    BMS_L1_FAIL_TYPE P1_Pressure;
    BMS_L1_FAIL_TYPE P2_Pressure;
    BMS_L1_FAIL_TYPE N1_Pressure;
    BMS_L1_FAIL_TYPE N2_Pressure;
    //
    BMS_L1_FAIL_TYPE PN1_Flow_Diff;
    BMS_L1_FAIL_TYPE PN2_Flow_Diff;
    //
    BMS_L1_FAIL_TYPE PN1_Pressure_Diff;
    BMS_L1_FAIL_TYPE PN2_Pressure_Diff;
    //
    BMS_L1_FAIL_TYPE A_Estack_Volt[6];
    //
    BMS_L1_FAIL_TYPE B_Estack_Volt[6];
    //
    BMS_L1_FAIL_TYPE A_Estack_Fault[6];
    //
    BMS_L1_FAIL_TYPE B_Estack_Fault[6];
    //
    BMS_L1_FAIL_TYPE Charge_Current;
    BMS_L1_FAIL_TYPE DisCharge_Current;
    //
    BMS_L1_FAIL_TYPE A_SOC;
    BMS_L1_FAIL_TYPE B_SOC;
} BMS_L1_FAIL_INST;

typedef struct
{
    BMS_L2_FAIL_TYPE P1_Electrolyte_Temp;
    BMS_L2_FAIL_TYPE P2_Electrolyte_Temp;
    BMS_L2_FAIL_TYPE N1_Electrolyte_Temp;
    BMS_L2_FAIL_TYPE N2_Electrolyte_Temp;
    //
    BMS_L2_FAIL_TYPE P1_StorageTank_Temp;
    BMS_L2_FAIL_TYPE P2_StorageTank_Temp;
    BMS_L2_FAIL_TYPE N1_StorageTank_Temp;
    BMS_L2_FAIL_TYPE N2_StorageTank_Temp;
    //
    BMS_L2_FAIL_TYPE P1_Flow;
    BMS_L2_FAIL_TYPE P2_Flow;
    BMS_L2_FAIL_TYPE N1_Flow;
    BMS_L2_FAIL_TYPE N2_Flow;
    //
    BMS_L2_FAIL_TYPE P1_Pressure;
    BMS_L2_FAIL_TYPE P2_Pressure;
    BMS_L2_FAIL_TYPE N1_Pressure;
    BMS_L2_FAIL_TYPE N2_Pressure;
    //
    BMS_L2_FAIL_TYPE PN1_Flow_Diff;
    BMS_L2_FAIL_TYPE PN2_Flow_Diff;
    //
    BMS_L2_FAIL_TYPE PN1_Pressure_Diff;
    BMS_L2_FAIL_TYPE PN2_Pressure_Diff;
    //
    BMS_L2_FAIL_TYPE A_Estack_Volt[6];
    //
    BMS_L2_FAIL_TYPE B_Estack_Volt[6];
    //
    BMS_L2_FAIL_TYPE A_Estack_Fault[6];
    //
    BMS_L2_FAIL_TYPE B_Estack_Fault[6];
    //
    BMS_L2_FAIL_TYPE Charge_Current;
    BMS_L2_FAIL_TYPE DisCharge_Current;
} BMS_L2_FAIL_INST;

typedef struct
{
    BMS_L3_FAIL_TYPE P1_Electrolyte_Temp;
    BMS_L3_FAIL_TYPE P2_Electrolyte_Temp;
    BMS_L3_FAIL_TYPE N1_Electrolyte_Temp;
    BMS_L3_FAIL_TYPE N2_Electrolyte_Temp;
    //
    BMS_L3_FAIL_TYPE P1_StorageTank_Temp;
    BMS_L3_FAIL_TYPE P2_StorageTank_Temp;
    BMS_L3_FAIL_TYPE N1_StorageTank_Temp;
    BMS_L3_FAIL_TYPE N2_StorageTank_Temp;
    //
    BMS_L3_FAIL_TYPE P1_Flow;
    BMS_L3_FAIL_TYPE P2_Flow;
    BMS_L3_FAIL_TYPE N1_Flow;
    BMS_L3_FAIL_TYPE N2_Flow;
    //
    BMS_L3_FAIL_TYPE P1_Pressure;
    BMS_L3_FAIL_TYPE P2_Pressure;
    BMS_L3_FAIL_TYPE N1_Pressure;
    BMS_L3_FAIL_TYPE N2_Pressure;
    //
    BMS_L3_FAIL_TYPE PN1_Flow_Diff;
    BMS_L3_FAIL_TYPE PN2_Flow_Diff;
    //
    BMS_L3_FAIL_TYPE PN1_Pressure_Diff;
    BMS_L3_FAIL_TYPE PN2_Pressure_Diff;
    //
    BMS_L3_FAIL_TYPE A_Estack_Volt[6];
    //
    BMS_L3_FAIL_TYPE B_Estack_Volt[6];
    //
    BMS_L3_FAIL_TYPE A_Estack_Fault[6];
    //
    BMS_L3_FAIL_TYPE B_Estack_Fault[6];
    //
    BMS_L3_FAIL_TYPE Charge_Current;
    BMS_L3_FAIL_TYPE DisCharge_Current;
    //
    BMS_L3_FAIL_TYPE P1_Freq;
    BMS_L3_FAIL_TYPE P2_Freq;
    BMS_L3_FAIL_TYPE N1_Freq;
    BMS_L3_FAIL_TYPE N2_Freq;
    //
    BMS_L3_FAIL_TYPE SOC_AB_Diff;
} BMS_L3_FAIL_INST;

// export
rt_bool_t bms_exception_occurred(void);
void bms_exception_event_reset(void);

#endif